package com.tiza.xiaoyoubao.ui.fragment;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.base.BaseFragment;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;
import com.tiza.xiaoyoubao.utils.DateUtils;

import org.simple.eventbus.Subscriber;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by mapple on 17/3/19.
 * 推广赚金
 */

public class PromoteMakeMoneyFragment extends BaseFragment {

    @BindView(R.id.rb_activationUser)
    RadioGroup rb_activationUser;
    //激活用户已接单
    @BindView(R.id.rb_order_user)
    RadioButton rb_order_user;
    //激活用户已完成
    @BindView(R.id.rb_complete_user)
    RadioButton rb_complete_user;
    //激活商家已接单
    @BindView(R.id.rb_order_buyer)
    RadioButton rb_order_buyer;
    //激活商家已接单
    @BindView(R.id.rb_complete_buyer)
    RadioButton rb_complete_buyer;
    @BindView(R.id.tv_invitationNum)
    TextView tv_invitationNum;
    @BindView(R.id.tv_rewardNum)
    TextView tv_rewardNum;
    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);
    private String num, bonus, orderUser, completeUser, orderBuyer, completeBuyer;
    Bundle args = new Bundle();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_promote_makemoney;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        getInvitationNum();
        getBonus();
        getBuyerInfoFinish();
        getBuyerinfo();
        getSellerinfo();
        getSellerinfoFinish();

    }


    /**
     * 跳转激活用户和激活商家成功
     */
    @Subscriber(tag = Constants.TAG_STATE_SUCCESS)
    private void loginSuccess(boolean pB) {
        rb_order_user.setChecked(false);
        rb_complete_user.setChecked(false);
        rb_order_buyer.setChecked(false);
        rb_complete_buyer.setChecked(false);
        rb_order_user.setTextColor(getResources().getColor(R.color.steel_blue));
        rb_complete_user.setTextColor(getResources().getColor(R.color.steel_blue));
        rb_order_buyer.setTextColor(getResources().getColor(R.color.steel_blue));
        rb_complete_buyer.setTextColor(getResources().getColor(R.color.steel_blue));
    }

    @OnClick({
            R.id.view_invitationUser,
            R.id.rb_order_user,
            R.id.rb_complete_user,
            R.id.rb_order_buyer,
            R.id.rb_complete_buyer,
            R.id.btn_share
    })
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.view_invitationUser:
                //UIHelper.showPromoteUser(mActivity);
                break;
            case R.id.rb_order_user://用户已接单
                args.putString("info", "order_user");
                args.putString("orderNum",orderUser);
                args.putString("completeNum",completeUser);
                UIHelper.showJiHuoUser(mActivity, args);
                rb_order_user.setTextColor(getResources().getColor(R.color.white));
                rb_complete_user.setTextColor(getResources().getColor(R.color.steel_blue));
                break;
            case R.id.rb_complete_user://用户已完成
                args.putString("info", "complete_user");
                args.putString("orderNum",orderUser);
                args.putString("completeNum",completeUser);
                UIHelper.showJiHuoUser(mActivity, args);
                rb_complete_user.setTextColor(getResources().getColor(R.color.white));
                rb_order_user.setTextColor(getResources().getColor(R.color.steel_blue));
                break;
            case R.id.rb_order_buyer://商家已发单
                args.putString("info", "order_buyer");
                args.putString("orderNum",orderBuyer);
                args.putString("completeNum",completeBuyer);
                UIHelper.showJiHuoBuyer(mActivity, args);
                rb_order_buyer.setTextColor(getResources().getColor(R.color.white));
                rb_complete_buyer.setTextColor(getResources().getColor(R.color.steel_blue));
                break;
            case R.id.rb_complete_buyer://商家已完成
                args.putString("info", "complete_buyer");
                args.putString("orderNum",orderBuyer);
                args.putString("completeNum",completeBuyer);
                UIHelper.showJiHuoBuyer(mActivity, args);
                rb_complete_buyer.setTextColor(getResources().getColor(R.color.white));
                rb_order_buyer.setTextColor(getResources().getColor(R.color.steel_blue));
                break;
            case R.id.btn_share:
                //获取剪贴板管理器：
                ClipboardManager cm = (ClipboardManager) mActivity.getSystemService(
                        Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                ClipData mClipData = ClipData.newPlainText("Label", "小优宝，轻松赚零花，随时随地，简单安全，记得注册时邀请填我的id:"+userInfo.getWebzzCmsBuyerInfo().getUserid()+"哦! http://t.cn/R9HtKk3 \n"+"\n\n\n请复制链接后粘贴到自带浏览器中打开并下载应用~");
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                toast("推广链接已复制,可以分享给微信和QQ好友哦!");
                break;
            default:
                break;
        }
    }

    //获取邀请的人数
    private void getInvitationNum() {
        ApiHttpClient.getInviteUser(PromoteMakeMoneyFragment.this, userInfo.getWebzzCmsBuyerInfo().getUserid(),
                                    new JsonCallback<String>(String.class, true) {

                                        @Override
                                        public void onBefore(Request request) {
                                            super.onBefore(request);
                                        }

                                        @Override
                                        public void onError(Call call, Exception e) {
                                            toast("信息获取失败");
                                        }

                                        @Override
                                        public void onResponse(ResponseInfo<String> response) {
                                            int code = response.getCode();
                                            String message = response.getMessage();
                                            if (code == 1) {
                                                showWarnDialog(message);
                                                return;
                                            }
                                            Map<String, Object> _map = response.getFirstEntity();
                                            num = String.valueOf(_map.get("number"));
                                            tv_invitationNum.setText(num);
                                        }

                                        @Override
                                        public void onAfter() {
                                            super.onAfter();
                                        }
                                    }
        );
    }

    //获取佣金
    private void getBonus() {
        ApiHttpClient.getBonus(PromoteMakeMoneyFragment.this, userInfo.getWebzzCmsBuyerInfo().getUserid(),
                               new JsonCallback<String>(String.class, true) {

                                   @Override
                                   public void onBefore(Request request) {
                                       super.onBefore(request);
                                   }

                                   @Override
                                   public void onError(Call call, Exception e) {
                                       toast("信息获取失败");
                                   }

                                   @Override
                                   public void onResponse(ResponseInfo<String> response) {
                                       int code = response.getCode();
                                       String message = response.getMessage();
                                       if (code == 1) {
                                           showWarnDialog(message);
                                           return;
                                       }
                                       Map<String, Object> _map = response.getFirstEntity();
                                       bonus = String.valueOf(_map.get("bonus"));
                                       tv_rewardNum.setText(DateUtils.formateRate(bonus));
                                   }

                                   @Override
                                   public void onAfter() {
                                       super.onAfter();
                                   }
                               }
        );
    }

    //激活商家已下单人数
    private void getSellerinfo() {
        ApiHttpClient.getSellerinfo(PromoteMakeMoneyFragment.this, userInfo.getWebzzCmsBuyerInfo().getUserid(), "1",
                                    new JsonCallback<String>(String.class, true) {

                                        @Override
                                        public void onBefore(Request request) {
                                            super.onBefore(request);
                                        }

                                        @Override
                                        public void onError(Call call, Exception e) {
                                            toast("信息获取失败");
                                        }

                                        @Override
                                        public void onResponse(ResponseInfo<String> response) {
                                            int code = response.getCode();
                                            String message = response.getMessage();
                                            if (code == 1) {
                                                showWarnDialog(message);
                                                return;
                                            }
                                            Map<String, Object> _map = response.getFirstEntity();
                                            orderBuyer = String.valueOf(_map.get("number"));
                                            rb_order_buyer.setText("已发单"+orderBuyer+"人");
                                        }

                                        @Override
                                        public void onAfter() {
                                            super.onAfter();
                                        }
                                    }
        );
    }

    //激活商家已完成人数
    private void getSellerinfoFinish() {
        ApiHttpClient.getSellerinfoFinish(PromoteMakeMoneyFragment.this, userInfo.getWebzzCmsBuyerInfo().getUserid(), "1",
                                          new JsonCallback<String>(String.class, true) {

                                              @Override
                                              public void onBefore(Request request) {
                                                  super.onBefore(request);
                                              }

                                              @Override
                                              public void onError(Call call, Exception e) {
                                                  toast("信息获取失败");
                                              }

                                              @Override
                                              public void onResponse(
                                                      ResponseInfo<String> response
                                              ) {
                                                  int code = response.getCode();
                                                  String message = response.getMessage();
                                                  if (code == 1) {
                                                      showWarnDialog(message);
                                                      return;
                                                  }
                                                  Map<String, Object> _map = response.getFirstEntity();
                                                  completeBuyer = String.valueOf(_map.get("number"));
                                                  rb_complete_buyer.setText("已完成"+completeBuyer+"人");
                                              }

                                              @Override
                                              public void onAfter() {
                                                  super.onAfter();
                                              }
                                          }
        );
    }

    //激活用户已下单人数
    private void getBuyerinfo() {
        ApiHttpClient.getBuyerInfo(PromoteMakeMoneyFragment.this, userInfo.getWebzzCmsBuyerInfo().getUserid(), "1",
                                   new JsonCallback<String>(String.class, true) {

                                       @Override
                                       public void onBefore(Request request) {
                                           super.onBefore(request);
                                       }

                                       @Override
                                       public void onError(Call call, Exception e) {
                                           toast("信息获取失败");
                                       }

                                       @Override
                                       public void onResponse(ResponseInfo<String> response) {
                                           int code = response.getCode();
                                           String message = response.getMessage();
                                           if (code == 1) {
                                               showWarnDialog(message);
                                               return;
                                           }
                                           Map<String, Object> _map = response.getFirstEntity();
                                           orderUser = String.valueOf(_map.get("number"));
                                           rb_order_user.setText("已接单"+orderUser+"人");
                                       }

                                       @Override
                                       public void onAfter() {
                                           super.onAfter();
                                       }
                                   }
        );
    }

    //激活用户已完成人数
    private void getBuyerInfoFinish() {
        ApiHttpClient.getBuyerInfoFinish(PromoteMakeMoneyFragment.this, userInfo.getWebzzCmsBuyerInfo().getUserid(), "1",
                                         new JsonCallback<String>(String.class, true) {

                                             @Override
                                             public void onBefore(Request request) {
                                                 super.onBefore(request);
                                             }

                                             @Override
                                             public void onError(Call call, Exception e) {
                                                 toast("信息获取失败");
                                             }

                                             @Override
                                             public void onResponse(ResponseInfo<String> response) {
                                                 int code = response.getCode();
                                                 String message = response.getMessage();
                                                 if (code == 1) {
                                                     showWarnDialog(message);
                                                     return;
                                                 }
                                                 Map<String, Object> _map = response.getFirstEntity();
                                                 completeUser = String.valueOf(_map.get("number"));
                                                 rb_complete_user.setText("已完成"+completeUser+"人");
                                             }

                                             @Override
                                             public void onAfter() {
                                                 super.onAfter();
                                             }
                                         }
        );
    }
}
